import { action5, action6 } from '@/pages/home/createAction';
import store from '@/pages/home/store';
import { Form, Input, Select } from 'antd';
import React, { useState } from 'react';
const { Option } = Select;
//Form-产品名称
const productName = (dataArray: any, newref: any) => {
  let TypeArr: any = [];
  dataArray.map((i: { productName: string }) => {
    if (TypeArr.includes(i.productName)) {
    } else TypeArr.push(i.productName);
  });
  return (
    <Form.Item
      label="产品(材料)名称"
      name="productName"
      rules={[{ required: true, message: '请选择产品名称' }]}
    >
      <Select
        disabled={store.getState().name == '编辑' ? true : false}
        defaultValue={''}
        onChange={(value, records: any) => {
          let productId = records.key;
          store.dispatch(action6(productId));
        }}
      >
        {dataArray.map((i: { productName: string; id: string }) => {
          return (
            <Option value={i.productName} key={i.id} children={null}></Option>
          );
        })}
      </Select>
    </Form.Item>
  );
};
//Form-小库id
const repoSeleted = (dataArray: []) => {
  //仓库id是根据文档的入库id，与产品id无关，如有增删这里需要更改
  let repoOtions: number[] = [];
  dataArray.map((i: { repoType: string }) => {
    switch (i.repoType) {
      case '辅料仓库': {
        repoOtions = [17, 18, 20];
        break;
      }
      case '肉鸡速冻库': {
        repoOtions = [21, 22, 23];
        break;
      }
      case '肉鸡冷藏库': {
        repoOtions = [24, 25, 26];
        break;
      }
    }
  });
  return (
    <Form.Item
      label="选择入库id"
      name="repoId"
      rules={[{ required: true, message: '请选择入库id' }]}
    >
      <Select
        defaultValue={''}
        //当新增入库时不禁用仓库选择，当编辑时禁用
        disabled={store.getState().name == '编辑' ? true : false}
      >
        {repoOtions.map((i) => {
          return <Option value={`${i}`} key={i} children={null}></Option>;
        })}
      </Select>
    </Form.Item>
  );
};
//form
// 订单编号，客户名 创建时间  件数 只数 重量 提货日期 提货人
// customerName numberOfPieces only weight averageWeight pickupDate pickupPerson species remark
const NewForm = (props: any) => {
  const Options = [
    {
      label: '辅料仓库',
      value: '1'
    },
    { label: '肉鸡速冻库', value: '2' },
    { label: '肉鸡冷藏库', value: '3' }
  ];
  const [dataArray, setDataArray]: any = useState([]);
  return (
    <Form ref={props.newref}>
      <Form.Item
        name="batchNumber"
        label="订单编号"
        rules={[{ required: true, message: '请输入订单编号' }]}
      >
        <Input></Input>
      </Form.Item>

      <Form.Item
        name="grade"
        label="请输入等级"
        rules={[{ required: true, message: '请选择等级' }]}
      >
        <Select>
          <Option value="A">A</Option>
          <Option value="B">B</Option>
          <Option value="C">C</Option>
          <Option value="不合格">不合格</Option>
        </Select>
      </Form.Item>
      <Form.Item
        label="仓库类型"
        name="repoType"
        rules={[{ required: true, message: '请选择仓库类型' }]}
      >
        <Select
          disabled={store.getState().name == '编辑' ? true : false}
          onChange={(value, record) => {
            //当发生该变的时候，通过表单去清空已选择的框value
            props.newref.current.setFieldsValue({
              productName: '',
              repoSeleted: ''
            });
            store.dispatch(action5(value));
            setDataArray(
              props.searchid.filter(
                (item: any) => item.repoType == store.getState().name
              )
            );
          }}
        >
          {Options.map((i) => (
            <Option value={i.label} key={i.value} children={null}></Option>
          ))}
        </Select>
      </Form.Item>

      {productName(dataArray, props.newref)}
      {repoSeleted(dataArray)}
      <Form.Item
        name="quantity"
        label="请选择数量（个）"
        rules={[{ required: true, message: '请选择数量' }]}
      >
        <Input></Input>
      </Form.Item>
      <Form.Item
        name="weight"
        label="请输入重量"
        rules={[{ required: true, message: '请输入重量' }]}
      >
        <Input></Input>
      </Form.Item>

      <Form.Item
        name="specification"
        label="请输入规格"
        rules={[{ required: true, message: '请输入规格' }]}
      >
        <Input></Input>
      </Form.Item>
      <Form.Item
        name="money"
        label="请输入单价"
        rules={[{ required: true, message: '请输入单价' }]}
      >
        <Input></Input>
      </Form.Item>
      <Form.Item
        name="supplier"
        label="请输入供应商"
        rules={[{ required: true, message: '请输入供应商' }]}
      >
        <Input></Input>
      </Form.Item>
      <Form.Item
        name="unit"
        label="请输入签收单位"
        rules={[{ required: true, message: '请输入签收单位' }]}
      >
        <Input></Input>
      </Form.Item>
    </Form>
  );
};
